<?php

namespace Yolo\Errors;

use Exception;

class RpcException extends Exception
{
    protected string $symbol = 'internal_error';

    protected int $statusCode = 500;

    protected array $metadata = [];

    protected array $responseMetadata = [];

    public function setStatusCode(int $statusCode): static
    {
        $this->statusCode = $statusCode;

        return $this;
    }

    public function addMetadata(array $metadata): static
    {
        $this->metadata = $metadata;

        return $this;
    }

    public function addResponseMetadata(array $metadata): static
    {
        $this->responseMetadata = $metadata;

        return $this;
    }

    public function getMetadata(): array
    {
        return $this->metadata;
    }

    public function getResponseMetadata(): array
    {
        return $this->responseMetadata;
    }

    public function getStatusCode(): int
    {
        return $this->statusCode;
    }

    public function getSymbol(): string
    {
        return $this->symbol;
    }

    public function json(): array
    {
        return [
            'code' => $this->getCode() ?: -1,
            'symbol' => $this->symbol,
            'message' => $this->getMessage(),
            'metadata' => $this->getResponseMetadata(),
            'http' => [
                'status' => $this->getStatusCode(),
            ]
        ];
    }
}
